package com.itcc.controller;

import com.itcc.exception.BusinessException;
import com.itcc.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.logging.Logger;

@RestControllerAdvice   //声明处理Rest风格的异常处理类
public class ProjectExceptionAdvice {

    private final Logger log = Logger.getLogger(this.getClass().getName());

    //处理业务异常
    @ExceptionHandler(BusinessException.class) //拦截异常
    public Result doException(BusinessException ex) {
        log.info("业务异常：" + ex.getClass().getName() + ":" + ex.getMessage());
        return new Result(ex.getCode(), null, ex.getMessage());
    }

    //处理系统异常
    @ExceptionHandler(SystemException.class) //拦截异常
    public Result doException(SystemException ex) {
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员
        log.info("系统异常：" + ex.getClass().getName() + ":" + ex.getMessage());
        return new Result(ex.getCode(), null, ex.getMessage());
    }

    //处理其他异常
    @ExceptionHandler(Exception.class) //拦截异常
    public Result doException(Exception e) {
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员
        log.info( "其他异常：" + e.getClass().getName() + ":" + e.getMessage());
        return new Result(Code.SYSTEM_UNKNOWN_ERR, null, "系统繁忙，请稍后再试！");
    }
}
